package com.example.a117.myapplication;


        import android.content.DialogInterface;
        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.password);
        //login button
        final Button login = (Button) findViewById(R.id.button);
        final String user = "admin";
        final String pass = "hello";




        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String username = "";
                EditText editText1 = (EditText)findViewById(R.id.editText);
                username = editText1.getText().toString();
                String password = "";
                EditText editText2 = (EditText)findViewById(R.id.editText2);
                password = editText2.getText().toString();

                SharedPreferences pre =getSharedPreferences("data",MODE_PRIVATE);

                if(username.equals(pre.getString("name",""))& password.equals(pre.getString("password",""))) {
                    Intent intent = new Intent(MainActivity.this,LifeDiary.class);
                    startActivity(intent);
                }
                else {
                    new AlertDialog.Builder(MainActivity.this).setTitle("错误").setMessage("账号或密码错误")
                            .setNegativeButton("OK",null)
                            .show();
                }
            }
        });
        //register button
        final Button register = (Button) findViewById(R.id.button2);
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //提示框确定是否跳转
                new AlertDialog.Builder(MainActivity.this).setTitle("注册").setMessage("确定进入注册界面？")
                        .setPositiveButton("是的", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(MainActivity.this, register.class);
                                startActivity(intent);
                            }})
                        .setNegativeButton("取消",null)
                        .show();
            }
        });
    }
}